package com.hotel.homehotel.before.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.homehotel.before.entiy.Room;
import com.hotel.homehotel.before.entiy.User;
import com.hotel.homehotel.before.entiy.UserHobby;
import com.hotel.homehotel.before.entiy.UserRole;
import com.hotel.homehotel.before.mapper.RoomMapper;
import com.hotel.homehotel.before.mapper.UserHobbyMapper;
import com.hotel.homehotel.before.mapper.UserMapper;
import com.hotel.homehotel.before.mapper.UserRoleMapper;
import com.hotel.homehotel.before.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl<T> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    UserHobbyMapper userHobbyMapper;
    @Autowired
    RoomMapper roomMapper;

    public  boolean  register(  User user){

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone_number",user.getPhoneNumber());
        User result = userMapper.selectOne(queryWrapper);
        if(result==null) {
            String password = new BCryptPasswordEncoder().encode(user.getPassword());
            user.setPassword(password);
             userMapper.insert(user);//注册成功
            String uid = userMapper.selectOne(queryWrapper).getId();//获得用户id
          UserRole userRole=  new UserRole();
          userRole.setUid(uid);
          userRole.setRid("2");
            userRoleMapper.insert(userRole);
            List<Room> room_category = roomMapper.selectList(new QueryWrapper<Room>().eq("room_category", user.getHobby()));

            for(Room r:room_category)
            {
                UserHobby userHobby=new UserHobby();
                userHobby.setRid(r.getId());
                userHobby.setUid(user.getId());
                userHobby.setNum(100);
                userHobbyMapper.insert(userHobby);
            }

            return  true;

        }
        return  false;


    }

    public  User getInfo(String phoneNumber){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone_number",phoneNumber);
        return  userMapper.selectOne(queryWrapper);
    }


    public  User getUserById(String id){
        return  userMapper.selectById(id);
    }

    public  boolean userUpdate(User user){
        List<Room> room_category = roomMapper.selectList(new QueryWrapper<Room>().eq("room_category", user.getHobby()));

        for(Room r:room_category)
        {
            UserHobby userHobby=new UserHobby();
            userHobby.setRid(r.getId());
            userHobby.setUid(user.getId());
            userHobby.setNum(100);
            userHobbyMapper.insert(userHobby);
        }

        return  userMapper.updateById(user)>0;

    }
    public  boolean userDelete(String id){

       return  userMapper.deleteById(id)>0;
    }
    public List<User> getAllUser()
    {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        return  userMapper.selectList(queryWrapper);
    }



}
